package com.eian.boot.module.system.role.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.eian.boot.common.constants.PermissionConstants;
import com.eian.boot.common.core.model.CommonResult;
import com.eian.boot.module.system.permission.model.vo.RolePermissionVO;
import com.eian.boot.module.system.role.model.dto.*;
import com.eian.boot.module.system.role.model.vo.RoleDetailVO;
import com.eian.boot.module.system.role.model.vo.RoleListVO;
import com.eian.boot.module.system.role.service.SysRoleService;
import com.eian.boot.mybatis.core.page.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "角色管理")
@RestController
@RequestMapping("/system/role")
@AllArgsConstructor
public class RoleController {
    private final SysRoleService roleService;

    @Operation(summary = "创建角色")
    @SaCheckPermission(PermissionConstants.Role.CREATE)
    @PostMapping("/create")
    public CommonResult<String> create(@Valid @RequestBody CreateRoleRequest request) {
        roleService.createRole(request);
        return CommonResult.ok();
    }

    @Operation(summary = "获取角色详情")
    @SaCheckPermission(PermissionConstants.Role.QUERY)
    @GetMapping("/{id}")
    public CommonResult<RoleDetailVO> getDetail(@Parameter(description = "角色ID") @PathVariable Long id) {
        return CommonResult.ok(roleService.getRoleDetail(id));
    }

    @Operation(summary = "分页查询角色列表")
    @SaCheckPermission(PermissionConstants.Role.LIST)
    @GetMapping("/list")
    public CommonResult<PageResult<RoleListVO>> list(@Valid RoleQueryRequest request) {
        return CommonResult.ok(roleService.getRoleList(request));
    }

    @Operation(summary = "更新角色信息")
    @SaCheckPermission(PermissionConstants.Role.UPDATE)
    @PostMapping("/update")
    public CommonResult<String> update(@Valid @RequestBody RoleUpdateRequest request) {
        roleService.updateRole(request);
        return CommonResult.ok();
    }

    @Operation(summary = "删除角色")
    @SaCheckPermission(PermissionConstants.Role.DELETE)
    @PostMapping("/delete")
    public CommonResult<String> delete(@Valid @RequestBody RoleDeleteRequest request) {
        roleService.delete(request);
        return CommonResult.ok("角色删除成功");
    }

    @Operation(summary = "分配角色权限")
    @SaCheckPermission(PermissionConstants.Role.ASSIGN_PERM)
    @PostMapping("/assign-perms")
    public CommonResult<String> assignPermission(@Valid @RequestBody RolePermissionAssignRequest request) {
        roleService.assignRolePermission(request);
        return CommonResult.ok();
    }

    @Operation(summary = "获取角色权限")
    @SaCheckPermission(PermissionConstants.Role.QUERY)
    @GetMapping("/{id}/perms")
    public CommonResult<RolePermissionVO> getRoleMenus(@Parameter(description = "角色ID") @PathVariable Long id) {
        return CommonResult.ok(roleService.getRolePermission(id));
    }

    @Operation(summary = "设置角色数据权限")
    @SaCheckPermission(PermissionConstants.Role.DATA_SCOPE)
    @PostMapping("/data-scope")
    public CommonResult<String> setDataScope(@Valid @RequestBody RoleDataScopeRequest request) {
        roleService.setRoleDataScope(request);
        return CommonResult.ok("数据权限设置成功");
    }

    @Operation(summary = "获取角色数据权限")
    @SaCheckPermission(PermissionConstants.Role.QUERY)
    @GetMapping("/{id}/data-scope")
    public CommonResult<RoleDataScopeRequest> getDataScope(@Parameter(description = "角色ID") @PathVariable Long id) {
        return CommonResult.ok(roleService.getRoleDataScope(id));
    }
}
